package cn.edu.neusoft.neufooddemo.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.adapter.FoodListAdapter;
import cn.edu.neusoft.neufooddemo.bean.FoodInfo;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.FoodModel;

public class SearchResultActivity extends BaseActivity implements RetrofitListener<List<FoodInfo>>{
    private RecyclerView recyclerView;
    private String search;
    private ImageView btn_return;

    protected void initViews() {
        setLayout(R.layout.activity_search_result);
        recyclerView = (RecyclerView) findViewById(R.id.rv);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        btn_return = (ImageView) findViewById(R.id.img_return);
    }

    public void initData() {
        Intent intent = getIntent();
        search = intent.getStringExtra("search");

        FoodModel foodModel = new FoodModel();
       foodModel.getFoodBySearch(search,this);
    }

    @Override
    public void onSuccess(List<FoodInfo> list, int flag) {
        if (list!=null&&list.size() > 0) {
            FoodListAdapter adapter = new FoodListAdapter(SearchResultActivity.this, list, R.layout.cardview_food);
            recyclerView.setAdapter(adapter);
        } else {
            showToast("没有搜索结果");
            finish();
        }
    }

    @Override
    public void onFail() {
        showToast("网络错误");
    }

    @Override
    void initEvents() {
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
